package in.muton.edushare.components.alertpanel;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.schedules.widgets.DateLabel;
import in.muton.edushare.url.URLLoader;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.UserAssignmentLinkDAO;
import com.edushare.common.vo.Assignments;
import com.edushare.common.vo.UserAssignmentLink;

public class AssgnAlert extends Panel {
	
	boolean isUserHomePage = false;
	
	public AssgnAlert(String id, UserAssignmentLink model) {
		super(id);
		isUserHomePage = true;
		loadPanel(model.getAssignment());
		
	}

	public AssgnAlert(String id, Assignments assgn) {
		super(id);
		loadPanel(assgn);
	}
	
	private void loadPanel(Assignments assgn){
		this.assModel = new CayenneModel<Assignments>(assgn);
		add(new Label("title", assgn.getAssignmentTitle()));
		add(new DateLabel("endDate", assgn.getEndDate(), 0));
		PageParameters params = new PageParameters();
		params.add("0", assgn.getAssignmentId().toString());
		add(new BookmarkablePageLink<Void>("submit", URLLoader
				.getPageClass(URLConstants.ASSIGNMENT_SUBMISSION), params));
		add(new BookmarkablePageLink<Void>("details", URLLoader
				.getPageClass(URLConstants.ASSIGNMENT_PATH), params));
		Link<Void> ignoreLink = new Link<Void>("ignore"){

			private static final long serialVersionUID = -3514056474030720383L;

			@Override
			public void onClick() {
				UserAssignmentLink ual = 
						new UserAssignmentLinkDAO(EduShareSession.get().getSessionORMInterface())
							.getUserAssignmentLink(EduShareSession.get().getLoggedInUser(), AssgnAlert.this.assModel.getObject());
				ual.setShowAlert(UserAssignmentLink.ALERT_DONT_SHOW);
				EduShareSession.get().getSessionORMInterface().commitAllChanges();
			}
			
		};
		
		add(ignoreLink);
		if(!isUserHomePage){
			ignoreLink.setVisible(isUserHomePage);
		}
	}

	private IModel<Assignments> assModel = null;
	private static final long serialVersionUID = 8091934630080210841L;
}
